<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/5/31
 * Time: 17:58
 */

namespace app\adminapi\validate;

use app\common\model\Order;

/**
 * 订单列表校验
 */
class OrderList extends Base
{
    protected $rule = [
        'page' => 'integer|min:1',
        'limit' => 'integer|min:1|max:20',
        'user_id' => 'integer|min:1|userExists',
        'goods_id' => 'integer|min:1|goodsExists',
        'status' => 'checkStatus',
        'goods_category_id' => 'integer|min:1|gcExists',
    ];

    protected $message = [
        'user_id.userExists' => '用户不存在',

        'goods_id.goodsExists' => '商品不存在',

        'status.checkStatus' => '状态错误',

        'goods_category_id.gcExists' => '商品分类不存在',
    ];

    /**
     * 状态校验
     */
    protected function checkStatus($value, $rule, $data)
    {
        if(!Order::getStatusText($value)) return false;
        return true;
    }

    /**
     * 商品分类是否存在
     */
    protected function gcExists($value, $rule, $data)
    {
        if(!db('goods_category')->where('id', $value)->count()) return false;
        return true;
    }

    /**
     * 商品是否存在
     */
    protected function goodsExists($value, $rule, $data)
    {
        if(!db('goods')->where('id', $value)->count()) return false;
        return true;
    }

    /**
     * 用户是否存在
     */
    protected function userExists($value, $rule, $data)
    {
        if(!db('user')->where('id', $value)->count()) return false;
        return true;
    }
}